<?php
namespace app\admin\controller;
use app\admin\controller\Base;
use app\admin\model\Admin;
use app\admin\model\News;
use app\admin\model\Setting;
<<<<<<< HEAD
use app\admin\model\Phototype;
=======
use app\admin\model\School;
>>>>>>> shool
use app\admin\model\Photo;
use think\Request;
use think\File;
class  Photos extends Base
{
     public $qr_src_path = "public/static/admin/uploads/video_qrcode/";
    /*
     * 视频列表
     */
     public function lists(Request $request){
         $user = $this->user_info();
<<<<<<< HEAD
         $video_type_list = Phototype::order("sort","asc")->select();
=======
         $video_type_list = School::order("sort","asc")->select();
>>>>>>> shool
         $video_type_id =  !empty($request->route("videotype")) ? $request->route("videotype") : $video_type_list[0]['id'];
         $data=[
           "videotype"=>$video_type_list,
           "setting" => $this->getAllSetting(),
           "videtype_id"=> $video_type_id ? $video_type_id : $video_type_list[0]["id"],
         ];
         return view("list",$data);
     }
     /*
      * 查询视频
      */
    public function videolist(Request $request){
       $res =  Photo::where(["videotype"=>$request->post("typeid")])->order("id","desc")->select();
       return json(collection($res)->toArray());
    }
    /*
     * 修改视频的显示状态
     */
    public function VideoStatus(Request $request){
        $user = $this->user_info();
        if(Photo::update($request->post())){
            $this->success("成功");
        }else{
            $this->error("失败");
        }
    }
    /*
     * 删除视频
     */
     public function video_del(Request $request){
       $data = $request->post();
         if(Photo::destroy($data['id'])){
              $this->success("删除成功","Photos/lists?videotype={$data['videotype']}");
         }else{
              $this->error("删除失败");
         }
     }
     /*
      * 批量上传校园生活照片
      */
      public function upload_cover_more(Request $request){
        //接收上传的文件
        $file = $this->request->file('file');
        $videotype = $request->param("videotype");
        if(!empty($file)){
          // 图片存的路径
          $imgUrl= ROOT_PATH . 'public' . DS .'static'. DS .'admin'. DS .'uploads'. DS .'photo';
          // 移动到框架应用根目录/public/uploads/ 目录下
<<<<<<< HEAD
          $info = $file->validate(['size'=>1048576*1024,'ext'=>'jpg,png,gif'])->rule('uniqid')->move($imgUrl);
=======
          $info = $file->validate(['size'=>1048576*1024,'ext'=>'jpg,png,gif,jpeg'])->rule('uniqid')->move($imgUrl);
>>>>>>> shool
          $error = $file->getError();
          //验证文件后缀后大小
          if(!empty($error)){
            dump($error);exit;
          }
          if($info){
            //获取上传后的图片的名字
               $imgName = $info->getFilename();
               $imgpath = "/public/static/admin/uploads/photo/"; //图片的完整路径
               $filename = $file->getInfo("name");
               $dotLastPosition = strrpos($filename,".");
               $title = substr($filename,0,$dotLastPosition);
               $arr = [
                 "title"=>$title,
                 "video_cover"=>$imgpath.$imgName,
                 "videotype"=>$videotype,
                 "description"=>$title,
                 "status"=>1,
                 "create_time"=>time(),
                 "update_time"=>time(),
                 "keywords"=>$title,
               ];
               if(Photo::create($arr)){
                 $response = [
                   "code"=>0,
                   "msg"=>"上传成功",
                   "data"=>[
                     "src"=>$imgpath.$imgName
                   ],
                 ];
                 return json($response);
               }

          }else{
            // 上传失败获取错误信息
            $file->getError();
          }
        }else{
          $photo = '';
        }
        if($photo !== ''){
          return ['code'=>1,'msg'=>'成功','photo'=>$photo];
        }else{
          return ['code'=>0,'msg'=>'失败'];
        }
      }
 //普通上传
  public function upload_cover(Request $request){
    //接收上传的文件
    $file = $this->request->file('file');

    if(!empty($file)){
      //图片存的路径
      $imgUrl= ROOT_PATH . 'public' . DS .'static'. DS .'admin'. DS .'uploads'. DS .'photo';
      // 移动到框架应用根目录/public/uploads/ 目录下
      $info = $file->validate(['size'=>1048576*1024,'ext'=>'jpg,png,gif'])->rule('uniqid')->move($imgUrl);
      $error = $file->getError();
      //验证文件后缀后大小
      if(!empty($error)){
        dump($error);exit;
      }
      if($info){
        // 成功上传后 获取上传信息
        //获取图片的名字
        $imgName = $info->getFilename();
        //获取图片的路径
        $photo= $imgName;
      }else{
        // 上传失败获取错误信息
        $file->getError();
      }
    }else{
      $photo = '';
    }
    if($photo !== ''){
      return ['code'=>1,'msg'=>'成功','photo'=>$photo];
    }else{
      return ['code'=>0,'msg'=>'失败'];
    }
  }
      /*
       * 添加视频
       */
      public function addVideo(Request $request){
        $data = $request->post();
        $data["create_time"] = time();
        $data["update_time"] = time();
        $res = Photo::create($data);
        if($res){
          $this->success("添加成功","Photos/lists?videotype={$data['videotype']}");
        }else{
          $this->error("添加视频失败");
        }
      }
    /*
     * 修改视频
     */
    public function update_video(Request $request){
      $data = $request->post();
      $data["update_time"] = time();
      if(Photo::update($data)){
        $this->success("修改成功","Photos/lists?videotype={$data['videotype']}");
      }else{
        $this->error("修改视频失败");
      }
    }
    /*-----------------------------------------------------------------------------------------*/
    //视频分类列表
    public function type_list(){
        $user = $this->user_info();
<<<<<<< HEAD
        $resource = collection(Phototype::order("sort asc")->select())->toArray();
=======
        $resource = collection(School::order("sort asc")->select())->toArray();
>>>>>>> shool
        foreach($resource as $k => $v){
            $sum = Photo::where(array("Videotype"=>$v['id']))->count();
            $resource[$k]['sum'] = $sum;
        }
        $data = array(
            "setting" => $this->getAllSetting(),
            "list"=>$resource,
        );
        return view("typelist",$data);
    }
    //添加
    public function type_addform(){
        $user = $this->user_info();
        $data = array(
            "setting" => $this->getAllSetting(),
        );
        return view("type_addform",$data);
    }
    public function type_add(Request $request){
        //接收所有的请求数据，数组：多个数据的集合
        $data = $request->post();
        // 创建数组元素
        $data['create_time'] = time();
        $data['update_time'] = time();
<<<<<<< HEAD
        $res = Phototype::insert($data);  //执行数据库命令
=======
        $res = School::insert($data);  //执行数据库命令
>>>>>>> shool
        //返回结果的判断
        if($res){
            $this->success("添加成功","Photos/type_list");
        }else{
            $this->error("添加失败","Photos/type_addform");
        }
    }

    //删除分类
    public function type_deletes(Request $request){
        $user = $this->user_info();
        $id = $request->post('id');
        //删除分类的时先去新闻标中查看该分类下是否有新闻，如果有则不允许删除
        $sum =  Photo::where(["videotype"=>$id])->count();
        if($sum > 0){
            $this->error("该分类有视频，请将视频迁移之后或删除该分类下的所有视频后在删除分类！");
            exit();
        }
        //如果该分类下没有新闻则直接删除
<<<<<<< HEAD
        if(Phototype::destroy($id)){
=======
        if(School::destroy($id)){
>>>>>>> shool
            $this->success("删除成功","Photos/type_list");
        }else{
            $this->error("删除失败");
        }
    }
    //修改分类状态
    public function setTypeStatus(Request $request){
        $user = $this->user_info();
<<<<<<< HEAD
        if(Phototype::update($request->post())){
=======
        if(School::update($request->post())){
>>>>>>> shool
            $this->success("成功","Photos/type_list");
        }else{
            $this->error("失败");
        }
    }
    //修改分类
    public function type_update(Request $request){
        $user = $this->user_info();
        $id = $request->get("id");
        $data = array(
            "setting" => $this->getAllSetting(),
<<<<<<< HEAD
            "data" => Phototype::where(array("id"=>$id))->find(),
=======
            "data" => School::where(array("id"=>$id))->find(),
>>>>>>> shool
        );
        return view("type_update",$data);
    }
    public function type_toUpdate(Request $request){
        $user = $this->user_info();
        $data = $request->post();
        $data['update_time'] = time();
<<<<<<< HEAD
        if(Phototype::update($data)){
=======
        if(School::update($data)){
>>>>>>> shool
            $this->success("修改成功","Photos/type_list");
        }else{
            $this->error("修改失败");
        }
    }


}
